package old.proxy.demo3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class InvocationHandelProxy implements InvocationHandler {

    Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //获取代理
    Object getProxy(){
        /*
         *  * <pre>{@code
         *     InvocationHandler handler = new MyInvocationHandler(...);
         *     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
         *                                          new Class<?>[] { Foo.class },
         *                                          handler);
         * }</pre>
         * */
        var obj = Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
        return  obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(method.getName());
        Object obj = method.invoke(target,args);
        return obj;
    }
    void log(String msg){
        System.out.println("调用了===>" + msg);
    }


}
